package com.kh.first.utils;

import sun.misc.Unsafe;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class UserUtils {
    public static final String KH_USER = "kh-user";

    public static final String THEME = "kit-admin/";
    public static final String THEME_OA = "smeoa/";

    public static final String isAdmin = "-admin";
    public static final String nameCN = "-nameCN";
    static Object object = new Object();

    public static String getUserPinYin() {
            HttpSession session = ContextHolderUtils.getSession();
            return (String) session.getAttribute(session.getId());
    }

    public static void setUserPinYin(String userPinYin, HttpServletRequest request) {

        HttpSession session = request.getSession();
        session.setAttribute(session.getId(), userPinYin);
    }

    public static String getUserName() {
        HttpSession session = ContextHolderUtils.getSession();
        return (String) session.getAttribute(session.getId() + nameCN);
    }

    public static void setUserName(String name, HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute(session.getId() + nameCN, name);
    }

    public static void setAdminStatus(Long[] departs, HttpServletRequest request) {
        HttpSession session = request.getSession();
        for (Long depart : departs) {
            if (129 == depart || 130 == depart) {//129信息科,130
                session.setAttribute(session.getId() + isAdmin, true);
                return;
            }
        }
        session.setAttribute(session.getId() + isAdmin, false);
    }

    public static Boolean getAdminStatus() {
        HttpSession session = ContextHolderUtils.getSession();
        return (Boolean) session.getAttribute(session.getId() + isAdmin);
    }
//	public static String getStaffNo() {
//		return getUser().getStaffNo();
//	}

    public static int getDateMax() {
        Calendar instance = Calendar.getInstance();
//		instance.set(2018, 1, 1);
//		int i = instance.get(Calendar.MONTH);//当前月份
        return instance.getActualMaximum(Calendar.DATE);

    }

    public static String getMonth() {
        SimpleDateFormat dateformat = new SimpleDateFormat("MM");
        return dateformat.format(new Date());
    }

    public static String getYear() {
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy");
        return dateformat.format(new Date());
    }
}
